﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;

namespace ToolExcelPub
{
    public class CToolConfig
    {
        //配置目录
        public string PathConfig = "Config";
        //输出目录
        public string PathOutput = "Config";
        //表格目录
        public string PathExcel = "Excel";
        //JsonServer目录
        public string PathJsonServer = "JsonServer";
        //JsonServer目录
        public string PathJsonClient = "JsonClient";
        //Json目录
        public string PathJson = "Json";
        //Csharp目录
        public string PathCsharp = "Csharp";
        //Ts目录
        public string PathTs = "Ts";
        //Go目录
        public string PathGo = "Go";

    }
    public static partial class XTool
    {
        //导出Csharp
        public static bool OutputCsharpNeed = true;
        //导出TypeScript
        public static bool OutputTsNeed = true;
        //导出Go
        public static bool OutputGoNeed = true;


        public static string ToolConfigFileName = @"\ToolConfig.json";
        public static CToolConfig ToolConfig = new CToolConfig();

        public static JsonSerializerOptions JsonOpt = new JsonSerializerOptions();
        


        public static void InitJson()
        {
            ShowInfo("InitJson!");

            //日期格式化
            //JsonOpt.Converters.Add(new SystemTextJsonConvert.DateTimeConverter());
            //设置支持中文的unicode编码
            JsonOpt.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
            //启用驼峰格式
            //JsonOpt.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
            //启用缩进设置
            JsonOpt.WriteIndented = true;
            //包含Fields
            JsonOpt.IncludeFields = true;
        }

        public static void InitCfg()
        {
            ExcelPackage.LicenseContext = LicenseContext.NonCommercial;

            InitJson();
            LoadCfg();
            WriteCfg();
        }

        public static void LoadCfg()
        {
            ShowInfo("LoadCfg!");
            try
            {
                var path = Directory.GetCurrentDirectory() + ToolConfigFileName;
                if (File.Exists(path))
                {
                    FileStream fs = new FileStream(path, FileMode.Open);
                    StreamReader sr = new StreamReader(fs, Encoding.Default);

                    var jsonString = sr.ReadToEnd();
                    ToolConfig = JsonSerializer.Deserialize<CToolConfig>(jsonString, JsonOpt);

                    sr.Close();
                }
            }
            catch (Exception ex)
            {
                ShowError($"ToolConfigRead 错误 {ex.Message}", "提示");
            }
        }
        public static void WriteCfg()
        {
            ShowInfo("WriteCfg!");
            try
            {
                var path = Directory.GetCurrentDirectory() + ToolConfigFileName;

                var jsonString = JsonSerializer.Serialize(ToolConfig, JsonOpt);

                //dynamic p = new ExpandoObject();
                //((IDictionary<string, object>)p).Add("Id", 33);
                //((IDictionary<string, object>)p).Add("Name", "aaaa");
                //((IDictionary<string, object>)p).Add("Age", 55);
                //Dictionary<int, dynamic> dict = new Dictionary<int, dynamic>();
                //dict[1] = p;
                //jsonString = JsonSerializer.Serialize(dict, JsonOpt);
                //Dictionary<int, dynamic> dict2 = JsonSerializer.Deserialize<Dictionary<int, dynamic>>(jsonString, JsonOpt);

                ToolGlobal.WriteNewFile(path, jsonString.ToString());
            }
            catch (Exception ex)
            {
                ShowError($"ToolConfigWrite 错误 {ex.Message}", "提示");
            }
        }
    }
}
